package com.JobFind.util;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 过滤器，如果未登录或超时，则重定向到指的登录页面
 * 
 */
public class SessionFilterUtil implements Filter {

	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
			FilterChain filterChain) throws IOException, ServletException {

		HttpServletRequest request = (HttpServletRequest) servletRequest;//将请求转化成HttpServletResponse 
		HttpServletResponse response = (HttpServletResponse) servletResponse;
		String currentURL = request.getRequestURI(); // 取得根目录所对应的绝对路径
		String checkURL = currentURL.substring(currentURL.lastIndexOf("/"), currentURL.length());
		HttpSession session = request.getSession(false);
		/*String type = request.getHeader("X-Requested-With");*/
		//System.out.println(checkURL+"---");
		try {
			if (!"/user_login.html".equals(checkURL) && !"/company_login.html".equals(checkURL) && !"/userRegister.html".equals(checkURL) && !"/companyRegister.html".equals(checkURL) && !"/checkLog.html".equals(checkURL)) {
				if (session == null || session.getAttribute("loginusersession") == null) {
						response.sendRedirect("http://localhost:8080/FindJob/user_login.html");
						return;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
		}
		filterChain.doFilter(request, response);
	}

	public void destroy() {
	}

	public void init(FilterConfig filterConfig) throws ServletException {
	}
}